package com.ewhale.feitengguest.ui.user.mine

import android.os.Bundle
import android.view.View
import butterknife.OnClick
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.ArticleDetailDto
import com.ewhale.feitengguest.dto.LoginInfoDto
import com.ewhale.feitengguest.presenter.user.mine.MinePresenter
import com.ewhale.feitengguest.receiver.TagAliasOperatorHelper
import com.ewhale.feitengguest.receiver.TagAliasOperatorHelper.TagAliasBean
import com.ewhale.feitengguest.ui.WebViewActivity
import com.ewhale.feitengguest.ui.auth.LoginActivity
import com.ewhale.feitengguest.ui.shop.ShopMainActivity
import com.ewhale.feitengguest.utils.Contants
import com.ewhale.feitengguest.view.user.mine.MineView
import com.orhanobut.hawk.Hawk
import com.simga.library.activity.MBaseFragment
import com.simga.library.utils.HawkKey
import com.simga.library.utils.StringUtil
import com.simga.library.utils.glide.GlideUtil
import kotlinx.android.synthetic.main.fragment_mine.*

class MineFragment : MBaseFragment<MinePresenter?>(), MineView {


    override fun getViewId(): Int {
        return R.layout.fragment_mine
    }

    override fun init(savedInstanceState: Bundle?) {}
    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String, message: String) {
        showErrorMsg(code, message)
    }

    @OnClick(R.id.tv_setting, R.id.rl_mytask, R.id.rl_mywallet,
            R.id.rl_invite, R.id.ll_user_info, R.id.rl_xuanshang, R.id.rl_aboutus)
    fun onViewClicked(view: View) {
        if (view.id != R.id.rl_call_service) {
            val isLogin = Hawk.get(HawkKey.IS_USER_LOGIN, false)
            if (!isLogin) {
                LoginActivity.open(mContext)
                return
            }
        }
        when (view.id) {
            R.id.tv_setting -> SettingActivity.open(mContext)
            R.id.ll_user_info -> UserInfoActivity.open(mContext)
            R.id.rl_mytask -> MyTaskActivity.open(mContext)
            R.id.rl_mywallet -> //                int status = Hawk.get(HawkKey.USER_AUTH_STATUS, 0);
                MyWalletActivity.open(mContext)
            R.id.rl_invite -> InviteActivity.open(mContext)
            R.id.rl_aboutus -> presenter!!.loadArticleDetails(3)
            R.id.rl_xuanshang -> if (!Hawk.get(HawkKey.IS_SHOP_LOGIN, false)) {
                LoginActivity.open(mContext)
            } else {
                Hawk.put(HawkKey.NOW_APP_STATUS, Contants.SHOP_STATUS)
                ShopMainActivity.open(mContext)
                //重新设置极光推送
                val tagAliasBean = TagAliasBean()
                tagAliasBean.run {
                    action = TagAliasOperatorHelper.ACTION_SET
                    TagAliasOperatorHelper.sequence++
                    alias = Hawk.get(HawkKey.SHOP_AUTHENTICATION)
                    isAliasAction = true
                    TagAliasOperatorHelper.getInstance().handleAction(context, TagAliasOperatorHelper.sequence, tagAliasBean)
                }
                //退出IM的登录
//                    AuthHelper.getInstance().logout(mContext, null);
                finish()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        val isLogin = Hawk.get(HawkKey.IS_USER_LOGIN, false)
        if (!isLogin) {
            iv_head!!.setImageResource(R.drawable.default_image)
            tv_phone!!.text = ""
            tv_name!!.text = "请登录"
        } else {
            val dto = Hawk.get<LoginInfoDto?>(HawkKey.USER_LOGIN_INFO, null)
            if (dto != null) {
                GlideUtil.loadPicture(dto.headPhoto, iv_head, R.drawable.default_image)
                tv_phone!!.text = StringUtil.phone2Unknown(dto.phone)
                tv_name!!.text = dto.nickName
            }
        }
    }

    override fun showDetail(result: ArticleDetailDto) {
        WebViewActivity.open(mContext, "关于我们", result.content)
    }
}